//package com.yuan.spring.redis.common;
//
//import com.alibaba.fastjson.JSONObject;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.Signature;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Component;
//
//import java.lang.reflect.Method;
//import java.util.concurrent.TimeUnit;
//
///**
// * redis 切面配置
// * Created by 袁恩光 on 2017/5/22.
// */
//@Component
//@Aspect
//public class AspectConfig {
//
//
//    @Autowired
//    private RedisTemplate redisTemplate;
//
//    @Pointcut("@annotation(com.yuan.spring.redis.common.RedisCache)")
//    public void aspectRedis() {
//
//    }
//
//    @Around("aspectRedis()")
//    @SuppressWarnings("unchecked")
//    public Object cut(ProceedingJoinPoint joinPoint) throws Throwable {
//        Object objectResult = null;
//        Signature signature = joinPoint.getSignature();
//        MethodSignature methodSignature = (MethodSignature) signature;
//        Method targetMethod = methodSignature.getMethod();
//        Object[] objects = joinPoint.getArgs();
//        String methodName = targetMethod.getName();
//        String className = joinPoint.getTarget().getClass().getTypeName();
//        String strKey = getKey(className, methodName, objects);
//        String hashKey = String.valueOf(strKey.hashCode());
//        RedisCache redisCache = targetMethod.getAnnotation(RedisCache.class);
//        String key = redisCache.value();
//        if (targetMethod.getReturnType().toString().equals("void")) {
//            return joinPoint.proceed(objects);
//        }
//        boolean flag;
//        switch (redisCache.cacheType()) {
//            case HASH:
//                flag = redisTemplate.opsForHash().hasKey(key, hashKey);
//                if (flag) {
//                    objectResult = JSONObject.parseObject(redisTemplate.opsForHash().get(key, hashKey).toString(), targetMethod.getReturnType());
//                } else {
//                    objectResult = joinPoint.proceed();
//                    redisTemplate.opsForHash().put(key, hashKey, JSONObject.toJSONString(objectResult));
//                }
//                break;
//            case VALUE:
//                Object object = redisTemplate.opsForValue().get(key + strKey);
//                if (null != object) {
//                    objectResult = JSONObject.parseObject(object.toString(), targetMethod.getReturnType());
//                } else {
//                    objectResult = joinPoint.proceed();
//                    redisTemplate.opsForValue().set(key + strKey, JSONObject.toJSONString(objectResult));
//                }
//                break;
//            default :
//        }
//        if (redisCache.expire() > 0) {
//            redisTemplate.expire(key, redisCache.expire(), TimeUnit.SECONDS);
//        }
//        return objectResult;
//    }
//
//    /**
//     * 生成key
//     *
//     * @param className  类名
//     * @param methodName 方法名
//     * @param values     方法值
//     * @return 生成的hashkey
//     */
//    private String getKey(String className, String methodName, Object[] values) {
//        StringBuilder sb = new StringBuilder();
//        sb.append(className);
//        sb.append(methodName);
//        for (Object object : values) {
//            sb.append(object);
//        }
//        return sb.toString();
//    }
//
//    /**
//     * 如果通过 aop找不到 method 的注解则用下面的方式
//     */
////    @Autowired
////    private RedisTemplate redisTemplate;
////
////    @Pointcut("@annotation(redisCache)")
////    public void aspectRedis(RedisCache redisCache) {
////
////    }
////
////    @Around("aspectRedis(redisCache)")
////    @SuppressWarnings("unchecked")
////    public Object cut(ProceedingJoinPoint joinPoint, RedisCache redisCache) throws Throwable {
////        Object objectResult = null;
////        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
////        Method targetMethod = methodSignature.getMethod();
////        Object[] objects = joinPoint.getArgs();
////        String hashKey = getKey(objects);
////        String key = redisCache.value();
////        if (targetMethod.getReturnType().toString().equals("void")) {
////            return joinPoint.proceed(objects);
////        }
////        boolean flag;
////        switch (redisCache.cacheType()) {
////            case HASH:
////                flag = redisTemplate.opsForHash().hasKey(key, hashKey);
////                if (flag) {
////                    objectResult = JSONObject.parseObject(redisTemplate.opsForHash().get(key, hashKey).toString(), targetMethod.getReturnType());
////                } else {
////                    objectResult = joinPoint.proceed();
////                    redisTemplate.opsForHash().put(key, hashKey, JSONObject.toJSONString(objectResult));
////                }
////                break;
////            case VALUE:
////                Object object = redisTemplate.opsForValue().get(key + key);
////                if (null != object) {
////                    objectResult = JSONObject.parseObject(object.toString(), targetMethod.getReturnType());
////                } else {
////                    objectResult = joinPoint.proceed();
////                    redisTemplate.opsForValue().set(key + key, JSONObject.toJSONString(objectResult));
////                }
////                break;
////        }
////        if (redisCache.expire() > 0) {
////            redisTemplate.expire(key, redisCache.expire(), TimeUnit.SECONDS);
////        }
////        return objectResult;
////    }
//
//}
